﻿/*
' Copyright (c) 2014  Christoc.com
'  All rights reserved.
' 
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
' 
*/

using System;
using System.Web.UI.WebControls;
using CorpGroup.Modules.cgDocuments.Components;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Services.Localization;
using DotNetNuke.UI.Utilities;
using System.Collections.Generic;
using Telerik.Web.UI.Widgets;
using Telerik.Web.UI.FileExplorer;
using Telerik.Web.UI;
using System.Web;

namespace CorpGroup.Modules.cgDocuments
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The View class displays the content
    /// 
    /// Typically your view control would be used to display content or functionality in your module.
    /// 
    /// View may be the only control you have in your project depending on the complexity of your module
    /// 
    /// Because the control inherits from DocumentsModuleBase you have access to any custom properties
    /// defined there, as well as properties from DNN such as PortalId, ModuleId, TabId, UserId and many more.
    /// 
    /// </summary>
    /// -----------------------------------------------------------------------------
    public partial class View : cgDocumentsModuleBase, IActionable
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    var dc = new DocumentController();
                    rptDocumentList.DataSource = dc.GetDocuments(ModuleId);
                    rptDocumentList.DataBind();
                    
                }
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        protected void rptDocumentListOnItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                var lnkEdit = e.Item.FindControl("lnkEdit") as HyperLink;
                var lnkDelete = e.Item.FindControl("lnkDelete") as LinkButton;
                var btnMoveUp = e.Item.FindControl("btnMoveUp") as Button;
                var btnMoveDown = e.Item.FindControl("btnMoveDown") as Button;
                var lnkDownload = e.Item.FindControl("lnkDownload") as HyperLink;

                var pnlAdminControls = e.Item.FindControl("pnlAdmin") as Panel;

                var t = (Document)e.Item.DataItem;
                
                //Set download link
                lnkDownload.NavigateUrl = HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + t.DocPath;
                //var tmp = HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.RawUrl;

                if (IsEditable && lnkDelete != null && lnkEdit != null && pnlAdminControls != null)
                {
                    pnlAdminControls.Visible = true;
                    lnkDelete.CommandArgument = t.DocId.ToString();
                    lnkDelete.Enabled = lnkDelete.Visible = lnkEdit.Enabled = lnkEdit.Visible = true;

                    //Supports popups
                    lnkEdit.NavigateUrl = EditUrl(string.Empty, string.Empty, "Edit", "tid=" + t.DocId);
                    //Does not support popups
                    //lnkEdit.NavigateUrl = DotNetNuke.Common.Globals.NavigateURL(base.TabId, "AddEdit", String.Format("mid={0}&tid={1}", base.ModuleId, t.ItemId));
                    ClientAPI.AddButtonConfirm(lnkDelete, Localization.GetString("ConfirmDelete", LocalResourceFile));

                    //Format enable MoveUp and MoveDown Buttons
                    btnMoveDown.CommandArgument = t.DocSortOrderId.ToString();
                    btnMoveDown.CommandName = "MoveDown";
                    btnMoveUp.CommandArgument = t.DocSortOrderId.ToString();
                    btnMoveUp.CommandName = "MoveUp";
                }
                else
                {
                    pnlAdminControls.Visible = false;
                }
            }
        }

        public void rptDocumentListOnItemCommand(object source, RepeaterCommandEventArgs e)
        {
            var tc = new DocumentController();
            if (e.CommandName == "MoveUp")
            {
                tc.MoveSortOrder(Convert.ToInt32(e.CommandArgument), ModuleId, -1);
            }
            if (e.CommandName == "MoveDown")
            {
                tc.MoveSortOrder(Convert.ToInt32(e.CommandArgument), ModuleId, 1);
            }
            if (e.CommandName == "Edit")
            {
                Response.Redirect(EditUrl(string.Empty, string.Empty, "Edit", "tid=" + e.CommandArgument));
            }
            if (e.CommandName == "Delete")
            {
                tc.DeleteDocument(Convert.ToInt32(e.CommandArgument), ModuleId);
            }
            Response.Redirect(DotNetNuke.Common.Globals.NavigateURL());
        }

        public ModuleActionCollection ModuleActions
        {
            get
            {
                var actions = new ModuleActionCollection
                    {
                        {
                            GetNextActionID(), Localization.GetString("EditModule", LocalResourceFile), "", "", "",
                            EditUrl(), false, SecurityAccessLevel.Edit, true, false
                        }
                    };
                return actions;
            }
        }
    }
}